package cn.iocoder.yudao.module.system.service.workappendage;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.module.system.dal.dataobject.workorder.WorkOrderDO;
import cn.iocoder.yudao.module.system.dal.mysql.workorder.WorkOrderMapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.system.controller.admin.workappendage.vo.*;
import cn.iocoder.yudao.module.system.dal.dataobject.workappendage.WorkAppendageDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.system.dal.mysql.workappendage.WorkAppendageMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.diffList;
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*;

/**
 * 工单附属物 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class WorkAppendageServiceImpl implements WorkAppendageService {

    @Resource
    private WorkAppendageMapper workAppendageMapper;
    @Resource
    private WorkOrderMapper workOrderMapper;

    @Override
    public Long createWorkAppendage(WorkAppendageSaveReqVO createReqVO) {
        // 插入
        WorkAppendageDO workAppendage = BeanUtils.toBean(createReqVO, WorkAppendageDO.class);
        WorkOrderDO workOrderDO = workOrderMapper.selectById(workAppendage.getWorkId());
        workAppendage.setCustomerId(workOrderDO.getCustomerId());
        workAppendageMapper.insert(workAppendage);
        // 返回
        return workAppendage.getId();
    }

    @Override
    public void updateWorkAppendage(WorkAppendageSaveReqVO updateReqVO) {
        // 校验存在
        validateWorkAppendageExists(updateReqVO.getId());
        // 更新
        WorkAppendageDO updateObj = BeanUtils.toBean(updateReqVO, WorkAppendageDO.class);
        workAppendageMapper.updateById(updateObj);
    }

    @Override
    public void deleteWorkAppendage(Long id) {
        // 校验存在
        validateWorkAppendageExists(id);
        // 删除
        workAppendageMapper.deleteById(id);
    }

    @Override
        public void deleteWorkAppendageListByIds(List<Long> ids) {
        // 校验存在
        validateWorkAppendageExists(ids);
        // 删除
        workAppendageMapper.deleteByIds(ids);
        }

    private void validateWorkAppendageExists(List<Long> ids) {
        List<WorkAppendageDO> list = workAppendageMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(WORK_APPENDAGE_NOT_EXISTS);
        }
    }

    private void validateWorkAppendageExists(Long id) {
        if (workAppendageMapper.selectById(id) == null) {
            throw exception(WORK_APPENDAGE_NOT_EXISTS);
        }
    }

    @Override
    public WorkAppendageDO getWorkAppendage(Long id) {
        return workAppendageMapper.selectById(id);
    }

    @Override
    public PageResult<WorkAppendageDO> getWorkAppendagePage(WorkAppendagePageReqVO pageReqVO) {
        return workAppendageMapper.selectPage(pageReqVO);
    }

}
